package com.sunlee.common.core.exception;

import com.sunlee.common.core.enums.ErrorCodeEnum;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * Excel操作异常，该异常会记录到数据库
 *
 * @author sunlee
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class ExcelException extends RuntimeException {

    private final int code;
    private final String msg;
    private final ErrorCodeEnum errorCodeEnum;


    /**
     * 推荐方式，会有Caused by
     */
    public ExcelException(ErrorCodeEnum errorCodeEnum, Throwable e) {
        super(e);
        this.errorCodeEnum = errorCodeEnum;
        this.code = errorCodeEnum.getCode();
        this.msg = errorCodeEnum.getMsg();
    }

    /**
     * 默认(Excel操作失败)为异常原因
     */
    public ExcelException(Throwable e) {
        super(e);
        this.errorCodeEnum = ErrorCodeEnum.EXCEL_OPERATE_FAIL;
        this.code = errorCodeEnum.getCode();
        this.msg = errorCodeEnum.getMsg();
    }

    /**
     * 不传Exception就不会有具体的Caused by
     */
    public ExcelException(ErrorCodeEnum errorCodeEnum) {
        super(errorCodeEnum.getMsg());
        this.errorCodeEnum = errorCodeEnum;
        this.code = errorCodeEnum.getCode();
        this.msg = errorCodeEnum.getMsg();
    }
}